package com.treetao.union.core.util;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.NoSuchAlgorithmException;
import java.util.Random;
import java.util.UUID;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.treetao.union.core.exception.DevMException;

/**
 * 本地上传
 */
@Component
public class FileUploadService{

	private static String[] letters = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E",
			"F" };
	
	// 获取文件存储路径（与fastdfs类似00/00/...jpg）
	public String getFileDir() {
		Random randData = new Random();
		int first = randData.nextInt(letters.length);// 保证0~F
		int second = randData.nextInt(letters.length);// 保证0~F
		return letters[first] + letters[second];
	}
	
	public String upload(File file,String savePath,String prefix) throws IOException,DevMException {
		String extName = FilenameUtils.getExtension(file.getName());
		String fileName = UUID.randomUUID() + extName;
		String midDir = getFileDir() + "/" + getFileDir();
		String filePath = savePath + "/" + midDir;
		File targetFile = new File(filePath);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		byte[] data = FileDigestUtil.getFileToByte(file);
		FileOutputStream out = new FileOutputStream(filePath + "/" + fileName);
		out.write(data);
		out.flush();
		out.close();
		
		String webUrl = prefix + "/" + midDir + "/" + fileName;
		return webUrl;
	}

	public String upload(MultipartFile file,String savePath,String prefix) throws IOException {
		String extName = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
		String fileName = UUID.randomUUID() + extName;
		// 路径：/opt/00/00/...jpg
		String midDir = getFileDir() + "/" + getFileDir();
		String filePath = savePath + "/" + midDir;
		File targetFile = new File(filePath);
		if (!targetFile.exists()) {
			targetFile.mkdirs();
		}
		byte[] data = file.getBytes();
		
		FileOutputStream out = new FileOutputStream(filePath + "/" + fileName);
		out.write(data);
		out.flush();
		out.close();

		String webUrl = prefix + "/" + midDir + "/" + fileName;
		return webUrl;
	}

	public static Boolean delete(String path) {
		File file = new File(path);
		if (file.exists()) {
			return file.delete();
		}
		return null;
	}

	public String uploadNetFile(String url,String savePath,String prefix) throws IOException, NoSuchAlgorithmException,DevMException {
		String extName = url.substring(url.lastIndexOf("."));
		String fileName = UUID.randomUUID() + extName;
		// String filePath = "classpath:static/upload/";
		String midDir = getFileDir() + "/" + getFileDir();
		String filePath = savePath + "/" + midDir;
		URL neturl = new URL(url);
		HttpURLConnection conn = (HttpURLConnection) neturl.openConnection();
		conn.connect();
		BufferedInputStream br = new BufferedInputStream(conn.getInputStream());
		byte[] buf = new byte[1024];
		int len = 0;
		FileOutputStream out = new FileOutputStream(filePath + fileName);
		while ((len = br.read(buf)) > 0)
			out.write(buf, 0, len);
		br.close();
		out.flush();
		out.close();
		conn.disconnect();
		File targetFile = new File(filePath + fileName);
		String webUrl = "";
		if (targetFile.exists()) {
			webUrl = prefix + "/" + midDir + "/" + fileName;
		}
		return webUrl;
	}

}
